package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.CustomerServiceCenterAdapter;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Dialog;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.CustomerServiceCenterModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.io.Serializable;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/24.
 * 客服中心
 */

public class CustomerServiceCenterActivity extends Activity implements View.OnClickListener ,Callback.CommonCallback<String>{
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.listview)
    ListView listview;

    @Bind(R.id.phone)//电话
    TextView phone;
    @Bind(R.id.time)//时间
            TextView time;
    @Bind(R.id.customer)//拨打电话
            TextView customer;

    List<CustomerServiceCenterModel> getproblem;
    CustomerServiceCenterAdapter adapter;

    Context context;
    private MyProgressDialog mDialog;
    AlertDialog alertDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_service_center);
        ButterKnife.bind(this);
        context=this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        customer.setOnClickListener(this);
        adapter = new CustomerServiceCenterAdapter(context);
        listview.setAdapter(adapter);
        processpindex();
        listview.setOnItemClickListener(clickListener);
    }
AdapterView.OnItemClickListener clickListener=new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent  intent= new Intent(context,HelpCenterActivity.class);
        intent.putExtra("number",i);
        intent.putExtra("data", (Serializable) getproblem);
    startActivity(intent);
    }
};


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.customer:
                if (TextUtils.isEmpty( phone.getText().toString().trim())){
                    ToastUtils.show(context, getResources().getString(R.string.unable));
                    return;
                }
                alertDialog= Dialog.phonedialog(context, phone.getText().toString().trim(), "", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(Intent.ACTION_DIAL);
                        Uri data = Uri.parse("tel:"+phone.getText().toString().trim());
                        intent.setData(data);
                        context.startActivity(intent);
                        alertDialog.dismiss();
                    }
                },"确定","取消");
                break;

        }
    }

    //客户信息
    private void processpindex(){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
            HttpClient.index(this);

        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }

    @Override
    public void onSuccess(String result) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        System.out.println("stringmd5result"+result);
        List<RegistrationModel> getnote = JsonTools.getunderlying(result);
        if (getnote!=null||getnote.size()>0){
            String status = getnote.get(0).getStatus();
            String getMsg = getnote.get(0).getMsg();
            if (status.equals("200")){
                String problem = getnote.get(0).getProblem();
                String info = getnote.get(0).getInfo();
                getproblem = JsonTools.getproblem(problem);
                List<CustomerServiceCenterModel> getinfo = JsonTools.getinfo(info);
                if (getinfo!=null||getinfo.size()>0){
                    phone.setText(getinfo.get(0).getCustomerservice());
                    time.setText(getinfo.get(0).getServicetime());
                }
                adapter.addItem(getproblem);
                ToastUtils.show(context, getMsg);
            }else if (status.equals("105")){
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, getMsg);
            }else {
                ToastUtils.show(context, getMsg);
            }
        }
    }
    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}
